package com.sluggard.security.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sluggard.common.model.res.Result;
import com.sluggard.mp.model.req.BaseReq;
import com.sluggard.mp.mvc.BaseController;
import com.sluggard.security.manage.model.entity.DictDataEntity;
import com.sluggard.security.manage.model.entity.DictTypeEntity;
import com.sluggard.security.manage.model.req.ChangeStatusReq;
import com.sluggard.security.manage.model.req.DictTypeDataReq;
import com.sluggard.security.manage.model.req.DictTypeReq;
import com.sluggard.security.manage.model.res.Option;
import com.sluggard.security.manage.service.IDictDataService;
import com.sluggard.security.manage.service.IDictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author lizheng
 * @since 2024-05-07
 */
@Tag(name = "字典管理")
@RestController
@RequestMapping("/manage/system/dict")
public class DictTypeController extends BaseController<DictTypeReq, DictTypeEntity, IDictTypeService> {

    @Resource
    private IDictDataService dataService;

    @Operation(description = "获取字典类型的字典值")
    @PostMapping("data")
    public Result<List<DictDataEntity>> data(@RequestBody DictTypeDataReq req) {
        return Result.ok(dataService.selectByDictType(req));
    }

    @Operation(description = "字典项")
    @PostMapping("options")
    public Result<List<Option>> options(@RequestBody DictTypeDataReq req) {
        return Result.ok(dataService.selectByDictType(req).stream()
                .map(item -> new Option(item.getDictValue(), item.getDictLabel(), item.getRemark()))
                .collect(Collectors.toList()));
    }

    @Override
    protected void onTables(QueryWrapper<DictTypeEntity> chainWrapper, Page<DictTypeEntity> page) {
        service.onTables(chainWrapper, page);
    }

    @Override
    protected List<DictTypeEntity> onList(QueryWrapper<DictTypeEntity> queryWrapper) {
        return service.onList(queryWrapper);
    }

    @Override
    public Result<DictTypeEntity> show(BaseReq req) {
        return Result.ok(service.show(req));
    }

    @Operation(summary = "启用/禁用")
    @PostMapping("changeStatus")
    public Result<Void> changeStatus(@Validated @RequestBody ChangeStatusReq req) {
        service.changeStatus(req);
        return Result.ok();
    }

    @Operation(summary = "保存字典项")
    @PostMapping("saveItems/{type}")
    public Result<Void> saveItems(@PathVariable("type") String type, @Validated @RequestBody List<DictDataEntity> req) {
        dataService.saveItems(type,req);
        return Result.ok();
    }
}
